import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../../services/httpsClient.dart';
import '../../../../models/message.dart';
import '../../../../services/storage.dart';

class RegisterStepThreeController extends GetxController {
  //TODO: Implement RegisterStepThreeController

  HttpsClient httpsClient = HttpsClient();

// 获取上个页面传过来的数据
  String tel = Get.arguments['tel'];
  String code = Get.arguments['code'];

  // 密码输入框的控制器
  TextEditingController passController = TextEditingController(); // 密码
  TextEditingController confirmPassController = TextEditingController(); // 确认密码

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  // 执行注册
  Future<MessageModel> doRegister() async {
    var res = await httpsClient.post('/api/register',
        data: {"tel": tel, "password": passController.text, "code": code});

    if (res != null) {
      if (res.data['success']) {
        print(res.data);
        // 注册成功执行登录，信息保存到本地
        Storage.setData("userinfo", res.data['userinfo']);
        return MessageModel(message: "注册成功", success: true);
      } else {
        return MessageModel(message: "注册失败", success: false);
      }
    } else {
      return MessageModel(message: "网络异常", success: false);
    }
  }
}
